package com.mohan.bean;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

@Component
public class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
    private ApplicationContext applicationContext;

    public Object getBeanByName(String beanName) {
        return applicationContext.getBean(beanName);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("applicationContext = " + applicationContext);
        this.applicationContext = applicationContext;
    }

    /**
     * 参数name：IOC容器创建当前对象时，为这个对象起的名字
     */
    @Override
    public void setBeanName(String s) {
        System.out.println("beanName = " + s);
    }

    /**
     * IOC容器会自动得将这个String值解析传给我们
     * @param resolver
     */
    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        String resolveStringValue = resolver.resolveStringValue("你好,${os.name},我的年龄是：#{20 * 18}");
        System.out.println("解析字符串为：" + resolveStringValue);
    }
}
